import Editor from 'wangeditor'
import axios from '@/libs/api.request'
export default {
  data () {
    return {
      $editorContent: '',
      $editor: null
    }
  },
  methods: {
    createEditor (el) {
      const editor = new Editor(el)
      this.$editor = editor
      editor.customConfig.onchange = html => {
        this.$editorContent = html
      }
      editor.customConfig.onblur = function (html) {
      }
      editor.customConfig.onfocus = function (html) {
      }
      editor.customConfig.zIndex = 100
      editor.customConfig.customAlert = info => {
        this.$Message.error(info)
      }
      editor.customConfig.uploadImgMaxLength = 1
      editor.customConfig.customUploadImg = (files, insert) => {
        this.uploadImg(files).then(data => {
          insert(data.full_img_url)
        }).catch(() => {
          this.$Message.error('图片上传失败')
        })
      }
      editor.create()
    },
    uploadImg (files) {
      return new Promise((resolve, reject) => {
        let fd = new FormData()
        fd.append('file', files[0])
        axios.request({
          url: this.$config.uploadImg,
          method: 'post',
          headers: {
            'access-token': '6a68b2d5-078e-4a00-967a-cd33f847973b',
            'employee-id': '00000000-0000-0000-0000-000000000000',
            'login-name': 'admin',
            'group-id': '',
            'Content-Type': 'multipart/form-data'
          },
          data: fd
        }).then(({
          data
        }) => {
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
